package com.gas.gm.pdu.msg;

import java.util.Iterator;
import java.util.List;

import com.gas.gm.model.Message;
import com.gas.gm.service.MessageService;
import com.gas.gm.util.GasContextHelper;

public class MessageStorage {
	
	private List<Message> messages;
	
	private MessageService service;
	
	public MessageStorage()
	{
		this.service=GasContextHelper.getApplicationContext().getBean(MessageService.class);
		this.messages=service.findAllMessages();
	}

	public void add(Message arg0) {
		synchronized (messages) {
			messages.add(arg0);
			service.createMessage(arg0);
		}
	}

	public Message get(int arg0) {
		synchronized (messages) {
			return messages.get(arg0);
		}
	}

	public void remove(Message arg0) {
		synchronized (messages) {
			if(messages.contains(arg0))
				service.removeMessage(arg0);
				messages.remove(arg0);
		}
	}

	public int size() {
		return messages.size();
	}
	
	public void print(){
		synchronized (messages) {
			Iterator<Message> iter=messages.iterator();
			while(iter.hasNext()){
				System.out.println(iter.next().toString());
			}
		}
	}
	
	

}
